package com.iscreen.utils.db.connection.mysql;

import java.sql.SQLException;

import com.iscreen.utils.db.connection.DBErrorHandler;
import com.iscreen.utils.exceptions.IScreenException;
import com.iscreen.utils.exceptions.IScreenExceptionCodes;

public class MySQLDBErrorHandler extends DBErrorHandler
{

	private int getExceptionCode(SQLException e)
	{
		//TODO Check if it's enough to look at getSQLState, and if so, remove DBConnection class and use java.sql.Connection directly
		switch (e.getErrorCode())
		{
			//Duplicate entry for primary key: 23000/1062
			case MySQLDBErrorCodes.ER_DUP_ENTRY : return IScreenExceptionCodes.DB_DUPLICATE_ENTRY; 
			default: return IScreenExceptionCodes.DB_GENERAL_ERROR; 
		}
	}
	@Override
	public void handleException(SQLException e) throws IScreenException
	{
		int exceptionCode = getExceptionCode(e);
		throw new IScreenException(exceptionCode, e);
	}
}
